package com.example.mengchun.viewpager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.mengchun.MainActivity;
import com.example.mengchun.Mutil;
import com.example.mengchun.R;

import org.feezu.liuli.timeselector.TimeSelector;

import java.util.ArrayList;
import java.util.List;

public class Viewpager extends AppCompatActivity {
    private View view1, view2, view3;
    private ViewPager viewPager;  //对应的viewPager

    private List<View> viewList;//view数组
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        if (getSharedPreferences("log",MODE_PRIVATE).getBoolean("dsa",true)){
            getSharedPreferences("log",MODE_PRIVATE).edit().putBoolean("dsa",false).commit();
        }else{
            startActivity(new Intent(Viewpager.this,MainActivity.class));
            finish();
        }
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        LayoutInflater inflater=getLayoutInflater();
        view1 = inflater.inflate(R.layout.layout1, null);
        view2 = inflater.inflate(R.layout.layout2,null);
        view3 = inflater.inflate(R.layout.layout3, null);
        viewList = new ArrayList<View>();
        view3.findViewById(R.id.setting_date).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //时间控件
                TimeSelector timeSelector = new TimeSelector(Viewpager.this, new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        SharedPreferences s = getSharedPreferences("user_date",MODE_PRIVATE);
                        SharedPreferences.Editor e = s.edit();
                        e.putString("date",time);
                        e.putString("xingzuo",Mutil.xingzuo(time.substring(5,7),time.substring(8,10)));
                        e.commit();
                        Mutil.xingzuo(time.substring(5,7),time.substring(8,10));
                        Toast.makeText(Viewpager.this, Mutil.xingzuo(time.substring(5,7),time.substring(8,10)), Toast.LENGTH_SHORT).show();
                    }
                }, "1994-01-01 00:00", "2017-12-31 23:59:59");
                timeSelector.show();
            }
        });
        view3.findViewById(R.id.jinru).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Viewpager.this, MainActivity.class));
            }
        });

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return viewList.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                // TODO Auto-generated method stub
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(viewList.get(position));


                return viewList.get(position);
            }
        };
        viewPager.setAdapter(pagerAdapter);

    }

}
